package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.MallProfitTemplateDao;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.MallProfitTemplate;
import com.sc.nft.entity.dto.MallProfitTemplateAdminDetailDTO;
import com.sc.nft.entity.dto.MallProfitTemplateAdminPageDTO;
import com.sc.nft.entity.vo.EditMallProfitTemplateVO;
import com.sc.nft.entity.vo.MallProfitTemplateAdminPageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.MallProfitTemplateService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.util.Beans;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商城分润模版 服务实现类
 * </p>
 *
 * @author ligen
 * @since 2024-01-09
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallProfitTemplateServiceImpl implements MallProfitTemplateService {

    private final MallProfitTemplateDao mallProfitTemplateDao;
    private final DigitalCollectionDao digitalCollectionDao;

    /**
     * 获取分润模版列表
     *
     * @param mallProfitTemplateAdminPageVO
     * @return
     */
    @Override
    public Page<MallProfitTemplateAdminPageDTO> getAdminPage(MallProfitTemplateAdminPageVO mallProfitTemplateAdminPageVO) {
        return mallProfitTemplateDao.getAdminPage(mallProfitTemplateAdminPageVO.getPageNo(), mallProfitTemplateAdminPageVO.getPageSize(), mallProfitTemplateAdminPageVO.getTemplateName(), mallProfitTemplateAdminPageVO.getTemplateType(), mallProfitTemplateAdminPageVO.getCreateAccount(), mallProfitTemplateAdminPageVO.getStatus());
    }

    /**
     * 获取分润模版详情
     *
     * @param id
     * @return
     */
    @Override
    public MallProfitTemplateAdminDetailDTO getAdminDetail(Long id) {
        MallProfitTemplate mallProfitTemplate = mallProfitTemplateDao.getById(id);
        MallProfitTemplateAdminDetailDTO mallProfitTemplateAdminDetailDTO = MallProfitTemplateAdminDetailDTO.build(mallProfitTemplate);
        if (!StringUtils.isEmpty(mallProfitTemplate.getCollectionId())) {
            List<String> names = Lists.newArrayList();
            List<Long> ids = JSONArray.parseArray(mallProfitTemplate.getCollectionId(), Long.class);
            for (Long collectionId : ids) {
                DigitalCollection digitalCollection = digitalCollectionDao.getById(collectionId);
                names.add(digitalCollection.getFirstTitle());
            }
            mallProfitTemplateAdminDetailDTO.setCollectionIds(ids);
            mallProfitTemplateAdminDetailDTO.setCollectionNames(names);
        }
        return mallProfitTemplateAdminDetailDTO;
    }

    /**
     * 编辑商城分润模版
     *
     * @param vo
     */
    @Override
    public void edit(Long sysMemberId, EditMallProfitTemplateVO vo) {
        vo.check();
        if (Objects.nonNull(vo.getId())) {
            MallProfitTemplate mallProfitTemplateInDb = getById(vo.getId());
            Assert.notNull(mallProfitTemplateInDb, () -> new GlobalRunTimeException(ErrorCode.THE_MOISTENING_TEMPLATE_DOES_NOT_EXIST));
            if (mallProfitTemplateInDb.getStatus()) {
                throw new GlobalRunTimeException(ErrorCode.THE_MOISTENING_TEMPLATE_HAS_BEEN_ENABLED);
            }
            Beans.copyPropertiesIgnoreNull(vo, mallProfitTemplateInDb);
            mallProfitTemplateInDb.setCollectionId(JSON.toJSONString(vo.getCollectionId()));
            mallProfitTemplateInDb.updateById();
        } else {
            MallProfitTemplate mallProfitTemplate = new MallProfitTemplate();
            BeanUtils.copyProperties(vo, mallProfitTemplate);
            mallProfitTemplate.setCollectionId(JSON.toJSONString(vo.getCollectionId()));
            mallProfitTemplate.setStatus(Boolean.FALSE);
            mallProfitTemplate.setCreateId(sysMemberId);
            mallProfitTemplate.insert();
        }
    }

    /**
     * 商城分润模版上架
     *
     * @param id
     */
    @Override
    public void enable(Long id) {
        MallProfitTemplate mallProfitTemplate = getById(id);
        Assert.notNull(mallProfitTemplate, () -> new GlobalRunTimeException(ErrorCode.THE_MOISTENING_TEMPLATE_DOES_NOT_EXIST));
        mallProfitTemplateDao.updateStatus(id, Boolean.TRUE);
    }

    /**
     * 商城分润模版下架
     *
     * @param id
     */
    @Override
    public void disable(Long id) {
        MallProfitTemplate mallProfitTemplate = getById(id);
        Assert.notNull(mallProfitTemplate, () -> new GlobalRunTimeException(ErrorCode.THE_MOISTENING_TEMPLATE_DOES_NOT_EXIST));
        mallProfitTemplateDao.updateStatus(id, Boolean.FALSE);
    }

    /**
     * 通过主键获取
     *
     * @param id
     * @return
     */
    @Override
    public MallProfitTemplate getById(Long id) {
        return mallProfitTemplateDao.getById(id);
    }
}
